﻿Imports LRS.BLL
Imports LRS.DTO
Imports System.Windows.Forms

Public Class ManageCustomer
    Public SelectedCustomerNumber As String = ""
    Private IsSelecting As Boolean = False
    Private IsAdding As Boolean = False
    Private oldRowIndex As Integer = -1
    Private Sub ManageCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gridCustomer.AutoGenerateColumns = False
        gridCustomer.DataSource = bCustomer.GetCustomers()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        gridCustomer.DataSource = Nothing
        gridCustomer.DataSource = bCustomer.GetCustomersByKeyword(txtSearch.Text)
    End Sub

    Public Sub SelectCustomer()
        btnSelect.Visible = True
        IsSelecting = True
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ucCustomer.ClearText()
        btnCancel.Enabled = True
        btnDelete.Enabled = False
        btnAdd.Enabled = False
        IsAdding = True
        gridCustomer.Enabled = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Not ucCustomer.ucValidateCust()) Then
            Return
        End If
        Dim cust As eCustomer = ucCustomer.GetCustomer()
        If (cust Is Nothing) Then
            Return
        End If
        If (IsAdding) Then
            bCustomer.Insert(cust)
        Else
            bCustomer.Update(cust)
        End If
        gridCustomer.Enabled = True
        gridCustomer.DataSource = Nothing
        gridCustomer.DataSource = bCustomer.GetCustomers()
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        btnCancel.Enabled = False
        IsAdding = False
        ucCustomer.Enabled = False
        Dim i = oldRowIndex
        oldRowIndex = -1
        gridCustomer.Rows(i).Cells("CustomerNumber").Selected = True
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (gridCustomer.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn dòng cần xóa")
            Return
        End If
        Dim row As Integer = gridCustomer.SelectedCells(0).RowIndex
        Dim result As DialogResult
        result = MessageBox.Show("Bạn có chắc chắn muốn xóa khách hàng có Số hiệu: " & gridCustomer.Rows(row).Cells("CustomerNumber").Value, "Xác nhận", MessageBoxButtons.OKCancel)
        If (result = DialogResult.OK) Then
            Dim custID As Integer = gridCustomer.Rows(row).Cells("CustomerID").Value
            bCustomer.DeleteCustomerByID(custID)
            gridCustomer.DataSource = Nothing
            gridCustomer.DataSource = bCustomer.GetCustomers()
            oldRowIndex = -1
        End If
    End Sub

    Private Sub gridCustomer_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridCustomer.RowEnter
        SelectedCustomerNumber = gridCustomer.Rows(e.RowIndex).Cells("CustomerNumber").Value
        ucCustomer.Enabled = True
        If (oldRowIndex = e.RowIndex) Then
            Return
        End If
        oldRowIndex = e.RowIndex
        Dim cust As eCustomer
        cust = bCustomer.GetCustomerByID(gridCustomer.Rows(e.RowIndex).Cells("CustomerID").Value)
        ucCustomer.SetCustomer(cust)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        gridCustomer.Enabled = True
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        IsAdding = False
        btnCancel.Enabled = False
        ucCustomer.Enabled = False
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        If (gridCustomer.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn một khách hàng")
            Return
        End If
        Dim row As Integer = gridCustomer.SelectedCells(0).RowIndex
        SelectedCustomerNumber = gridCustomer.Rows(row).Cells("CustomerNumber").Value
        IsSelecting = False
        Me.Close()
    End Sub

    Private Sub ManageCustomer_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (IsSelecting) Then
            MessageBox.Show("Vui lòng nhấn nút Chọn khách hàng này để chọn khách hàng")
            e.Cancel = True
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class